/**
 * 滚动球类
 * Created by zhd on 2017/6/23.
 */
var BallSprite = cc.Sprite.extend({
    _tag: null,
    _name: null,
    _step: 0,
    _isRoll: false,

    _distance: 0, // 移动位置
    _distance_speed: 0, // 速度
    _avg_distance: 0, // 平均速度
    _homig: 0,

    _frame: 5,

    ctor: function (index) {
        this._super();
        this.loadInit(index);
    },
    loadInit: function (index) {
        this._tag = GC.balls[index].tag;
        this._name = GC.balls[index].name;
        this.setSpriteFrame(this._name + "1.png");

        this.roll();
    },

    /**
     * 加载动画
     */
    loadAnimation: function () {
        this.stopAllActions();
        var frames = [];
        for (var i = 1; i < 10; i++) {
            var name = this._name + i + ".png";

            var frame = cc.spriteFrameCache.getSpriteFrame(name);
            frames.unshift(frame);
        }
        for (var i = 1; i < 5; i++) {
            for (var j = 0; j < 10; j++) {
                if (i == 4 && j > 8) {
                    continue;
                }
                var name = this._name + i + j + ".png";
                var frame = cc.spriteFrameCache.getSpriteFrame(name);
                frames.unshift(frame);
            }
        }
        var animation = new cc.Animation(frames, 0.03);
        animation.setRestoreOriginalFrame(true);
        var animate = new cc.animate(animation);
        this.runAction(animate.repeatForever());
    },

    /**
     * 滚动
     */
    roll: function () {
        this._isRoll = true;
        this.loadAnimation();
        this.scheduleUpdate();
    },

    /**
     * 停止滚动
     */
    stopRoll: function () {
        this._isRoll = false;
        this.stopAllActions();
        this.unscheduleAllCallbacks();
    },

    /**
     * 向前滚动
     * @param step
     */
    forward: function (step) {
        this.unscheduleAllCallbacks();
        this._distance = step;
        this._avg_distance = Math.floor(step / this._frame);
        this.schedule(this.updateMove);
    },

    /**
     * 向后滚动
     * @param step
     */
    back: function (step) {
        this.forward(-step);
    },

    /**
     * 从碰撞位置移到轨道的位置上
     */
    homing: function () {
        this.unscheduleAllCallbacks();
        this.schedule(this.updateHoming);
    },

    /**
     * 子弹移入轨道动画
     */
    updateHoming: function () {
        if(this._isRoll){
            this._step = this._step + 1;
        }
        this._homig++;

        var route = gameLayer._mapLayer._route[this._step];
        var radian = Math.atan2(route.x - this.x, route.y - this.y);

        var max_range = 0;
        if (route.x - this.x != 0) {
            max_range = (route.x - this.x) / Math.sin(radian);
        } else {
            max_range = Math.abs(route.x - this.x);
        }

        var r = Math.floor(max_range / this._frame) * this._homig;
        var x = this.x + r * Math.sin(radian);
        var y = this.y + r * Math.cos(radian);
        this.setPosition(x, y);

        if (this._homig == this._frame) {
            this.unscheduleAllCallbacks();
            if (this._isRoll) {
                this.roll();
            }
            gameLayer._mapLayer.checkSame();
            this._homig = 0;
        }
    },

    /**
     * 球移动
     */
    updateMove: function () {
        if (this._step < gameLayer._mapLayer._route.length) {
            var route = gameLayer._mapLayer._route[this._step];
            this.x = route.x;
            this.y = route.y;
            this.rotation = route.rotation;

            if (this._distance < 0) {
                if (Math.abs(this._distance) < this._avg_distance) {
                    this._distance_speed = -Math.abs(this._distance);
                    this._distance = 0;
                } else {
                    this._distance_speed = -this._avg_distance;
                    this._distance = this._distance + this._avg_distance;
                }
            } else if (this._distance > 0) {
                if (Math.abs(this._distance) < this._avg_distance) {
                    this._distance_speed = Math.abs(this._distance);
                    this._distance = 0;
                } else {
                    this._distance_speed = this._avg_distance;
                    this._distance = this._distance - this._avg_distance;
                }
            } else {
                this._distance_speed = 0;
                this.unscheduleAllCallbacks();
                if (this._isRoll) {
                    this.roll();
                }
            }

            if (this._isRoll) {
                this._step = this._step + 1 + this._distance_speed;
            } else {
                this._step = this._step + this._distance_speed;
            }
        } else {
            // 游戏结束
        }
    },

    /**
     * 球动画
     */
    update: function () {
        if (this._step < gameLayer._mapLayer._route.length) {
            var route = gameLayer._mapLayer._route[this._step];
            this.x = route.x;
            this.y = route.y;
            this.rotation = route.rotation;

            this._step = this._step + 1;
        } else {
            // 游戏结束
        }
    }

});